package com.dcm.order.service.impl;

import com.dcm.common.constant.Constants;
import com.dcm.common.exception.MyException;
import com.dcm.common.service.RedisService;
import com.dcm.common.utils.IdWorker;
import com.dcm.order.dto.AddressDto;
import com.dcm.order.dto.DistrictDto;
import com.dcm.order.entity.OrderEntity;
import com.dcm.order.mapper.OrderMapper;
import com.dcm.order.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * [订单实现类]
 *
 * @author : [pengrongsheng]
 * @createTime : [2021/3/30]
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RedisService redisService;
    @Value("${redis.database}")
    private String redisDatabase;
    @Value("${redis.expire.common}")
    private Long redisExpire;
    @Value("${redis.key.order}")
    private String redisKeyOrder;

    @Override
    public List<OrderEntity> findAll() {
        return orderMapper.findAll();
    }

    @Override
    public OrderEntity findOneById(Long id) {
        if (id == null || id < 0) {
            throw new MyException(Constants.ResponseMsg.VALIDATE_FAILED);
        }
        return orderMapper.findOneById(id);
    }

    @Override
    public DistrictDto findCityById(Long id) {
        return orderMapper.findCityById(id);
    }

    @Override
    public List<DistrictDto> findCityByPid(Long pid) {
        return orderMapper.findCityByPid(pid);
    }

    @Override
    public void insertOrderAddress(AddressDto address)  {
        if (StringUtils.isBlank(address.getReceiverName())) {
            throw new MyException(Constants.OrderErrorMsg.RECEIVER_CANNOT_NULL);
        }
        if (StringUtils.isBlank(address.getReceiverPhone())) {
            throw new MyException(Constants.OrderErrorMsg.PHONE_CANNOT_NULL);
        }
        if (StringUtils.isBlank(address.getReceiverDetailAddress())) {
            throw new MyException(Constants.OrderErrorMsg.DETAIL_ADDRESS_CANNOT_NULL);
        }

        //限制为10个字，限制输入汉字；
        String pattern1 = "/^[\\u4E00-\\u9FA5]{1,10}$/";
        //限制为11个字符，限制输入数字
        String patternPhone = "/^1[3-9]\\d{9}$/";
        //限制为80字，限制汉字数字、英文字母
        String pattern2 = "/^[\\u4e00-\\u9fa5_a-zA-Z0-9]{1,80}$/";
        boolean matchesName = address.getReceiverName().matches(pattern1);
        boolean matchesPhone = address.getReceiverPhone().matches(patternPhone);
        boolean matchesDetail = address.getReceiverDetailAddress().matches(pattern2);
        if (!(matchesName && matchesPhone && matchesDetail)) {
            throw new MyException(Constants.ResponseMsg.VALIDATE_FAILED);
        }
        String key = redisDatabase + ":" + redisKeyOrder + ":" + address.getId();
        redisService.set(key, address, redisExpire);
    }

    @Override
    public void insertOrder(OrderEntity order){
        long orderSn = new IdWorker().nextId();
        order.setOrderSn("orderSn:"+orderSn);
        order.setCreateTime(new Date());
        order.setId(new IdWorker().nextId());
        if (order.getMemberId() == null) {
            throw new MyException(Constants.ResponseMsg.VALIDATE_FAILED);
        }

        orderMapper.insertOrder(order);
    }

}
